/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package xml;

import java.io.File;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import target.Word;

/**
 *从XML文件中，随机得到一个满足条件的单词
 * @author GeH
 */
public class WordPumping {
   
    public WordPumping(){
    }

    public Word wordPumping(int rating, int status) {
        
        Word word = new Word();
        
        try {
            int num = -1; int a=0;int b=0;
            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read(new File("dic/UserList.xml"));

            //首先得到满足条件单词的总数
            List list = document.selectNodes("/wordbook/item");
            Iterator iter = list.iterator();
     
            while (iter.hasNext()) {
                a = 0 ;b = 0;
                Element itemElement = (Element) iter.next();
                Iterator iterator = itemElement.elementIterator("rating");
                while (iterator.hasNext()) {
                    Element ratingElement = (Element) iterator.next();
                    if(ratingElement.getText().equals(String.valueOf(rating)))
                    {
                        a = 1;
                    }
                }
                iterator = itemElement.elementIterator("status");
                 while (iterator.hasNext()) {
                    Element statusElement = (Element) iterator.next();
                    if(statusElement.getText().equals(String.valueOf(status)))
                    {
                        b = 1;
                    }
                }
                if ( a+b == 2)
                {
                    num++;
                }
                
            }
            if ( num == -1 )//说明没有符合条件的单词，返回空
                {
                    word = null;
                    return word;
                }
            
            //随机得到一个数字，即选择第几个满足条件的单词
            Random random = new Random();
            int r = random.nextInt(num+1);
            
            int count = -1;
            
            //得到此单词，返回类
            list = document.selectNodes("/wordbook/item");
            iter = list.iterator();
            
            while (iter.hasNext()) {
                a = 0 ;b = 0;
                Element itemElement = (Element) iter.next();
                Iterator iterator = itemElement.elementIterator("rating");
                while (iterator.hasNext()) {
                    Element ratingElement = (Element) iterator.next();
                    if(ratingElement.getText().equals(String.valueOf(rating)))
                    {
                        a = 1;
                    }
                }
                iterator = itemElement.elementIterator("status");
                 while (iterator.hasNext()) {
                    Element statusElement = (Element) iterator.next();
                    if(statusElement.getText().equals(String.valueOf(status)))
                    {
                        b = 1;
                    }
                }
                if ( a+b == 2)
                {
                    count++;
                }
                
                if ( count == r)
                {
                    if ( rating == 0)
                    {
                        word.status = word.status.DUMP;
                    }
                    if ( rating == 1)
                    {
                        word.status = word.status.REM;
                    }
                    if ( rating == 2)
                    {
                        word.status = word.status.NREM;
                    }
                    
                    word.rating = rating;
                    
                    iterator = itemElement.elementIterator("word");
                    while (iterator.hasNext()) {
                    Element wordElement = (Element) iterator.next();
                    word.word = wordElement.getText();
                    }
                    
                    iterator = itemElement.elementIterator("content");
                    while (iterator.hasNext()) {
                    Element contentElement = (Element) iterator.next();
                    word.content = contentElement.getText();
                    }
                    
                    iterator = itemElement.elementIterator("tricks");
                    while (iterator.hasNext()) {
                    Element tricksElement = (Element) iterator.next();
                    word.tricks = tricksElement.getText();
                    }
                    
                    iterator = itemElement.elementIterator("date");
                    while (iterator.hasNext()) {
                    Element dateElement = (Element) iterator.next();
                    word.date = dateElement.getText();
                    }
                    break;
                }
                
            }
            
            /*System.out.println(num);
            System.out.println(r);
            System.out.println(count);*/
        } 
        catch (Exception ex) {
            ex.printStackTrace();
        }
        
        return word;
    }
}

    
    

